package apis

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"go-admin/app/admin/models"
	"go-admin/app/admin/service"
	"go-admin/app/admin/service/dto"
	"go-admin/common/actions"
)

type Advertisements struct {
	api.Api
}

// GetPage 获取Advertisements列表
// @Summary 获取Advertisements列表
// @Description 获取Advertisements列表
// @Tags Advertisements
// @Param userId query int64 false "用户ID"
// @Param categoryId query int64 false "类别ID"
// @Param title query string false "标题"
// @Param description query string false "描述"
// @Param keyword query string false "关键词搜索(标题和描述)"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param createBy query int64 false "创建人"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.Advertisements}} "{"code": 200, "data": [...]}"
// @Router /api/v1/advertisements [get]
// @Security Bearer
func (e Advertisements) GetPage(c *gin.Context) {
	req := dto.AdvertisementsGetPageReq{}
	s := service.Advertisements{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]models.Advertisements, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取Advertisements失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取Advertisements
// @Summary 获取Advertisements
// @Description 获取Advertisements
// @Tags Advertisements
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.Advertisements} "{"code": 200, "data": [...]}"
// @Router /api/v1/advertisements/{id} [get]
// @Security Bearer
func (e Advertisements) Get(c *gin.Context) {
	req := dto.AdvertisementsGetReq{}
	s := service.Advertisements{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object models.Advertisements

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取Advertisements失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// IncrementClicks 增加广告点击次数
// @Summary 增加广告点击次数
// @Description 增加指定广告的点击次数
// @Tags Advertisements
// @Param id path int true "广告ID"
// @Success 200 {object} response.Response{data=string} "{\"code\": 200, \"data\": \"操作成功\"}"
// @Router /api/v1/advertisements/{id}/clicks/increment [post]
// @Security Bearer
func (e Advertisements) IncrementClicks(c *gin.Context) {
	req := dto.AdvertisementsGetReq{}
	s := service.Advertisements{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.IncrementClicks(int64(req.Id))
	if err != nil {
		e.Error(500, err, fmt.Sprintf("增加广告点击次数失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(nil, "增加点击次数成功")
}

// DecrementClicks 减少广告点击次数
// @Summary 减少广告点击次数
// @Description 减少指定广告的点击次数
// @Tags Advertisements
// @Param id path int true "广告ID"
// @Success 200 {object} response.Response{data=string} "{\"code\": 200, \"data\": \"操作成功\"}"
// @Router /api/v1/advertisements/{id}/clicks/decrement [post]
// @Security Bearer
func (e Advertisements) DecrementClicks(c *gin.Context) {
	req := dto.AdvertisementsGetReq{}
	s := service.Advertisements{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.DecrementClicks(int64(req.Id))
	if err != nil {
		e.Error(500, err, fmt.Sprintf("减少广告点击次数失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(nil, "减少点击次数成功")
}

// IncrementImpressions 增加广告展示次数
// @Summary 增加广告展示次数
// @Description 增加指定广告的展示次数
// @Tags Advertisements
// @Param id path int true "广告ID"
// @Success 200 {object} response.Response{data=string} "{\"code\": 200, \"data\": \"操作成功\"}"
// @Router /api/v1/advertisements/{id}/impressions/increment [post]
// @Security Bearer
func (e Advertisements) IncrementImpressions(c *gin.Context) {
	req := dto.AdvertisementsGetReq{}
	s := service.Advertisements{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.IncrementImpressions(int64(req.Id))
	if err != nil {
		e.Error(500, err, fmt.Sprintf("增加广告展示次数失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(nil, "增加展示次数成功")
}

// DecrementImpressions 减少广告展示次数
// @Summary 减少广告展示次数
// @Description 减少指定广告的展示次数
// @Tags Advertisements
// @Param id path int true "广告ID"
// @Success 200 {object} response.Response{data=string} "{\"code\": 200, \"data\": \"操作成功\"}"
// @Router /api/v1/advertisements/{id}/impressions/decrement [post]
// @Security Bearer
func (e Advertisements) DecrementImpressions(c *gin.Context) {
	req := dto.AdvertisementsGetReq{}
	s := service.Advertisements{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.DecrementImpressions(int64(req.Id))
	if err != nil {
		e.Error(500, err, fmt.Sprintf("减少广告展示次数失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(nil, "减少展示次数成功")
}

// Insert 创建Advertisements
// @Summary 创建Advertisements
// @Description 创建Advertisements
// @Tags Advertisements
// @Accept application/json
// @Product application/json
// @Param data body dto.AdvertisementsInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/advertisements [post]
// @Security Bearer
func (e Advertisements) Insert(c *gin.Context) {
	req := dto.AdvertisementsInsertReq{}
	s := service.Advertisements{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))
	req.UserId = 1
	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建Advertisements失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改Advertisements
// @Summary 修改Advertisements
// @Description 修改Advertisements
// @Tags Advertisements
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.AdvertisementsUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/advertisements/{id} [put]
// @Security Bearer
func (e Advertisements) Update(c *gin.Context) {
	req := dto.AdvertisementsUpdateReq{}
	s := service.Advertisements{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改Advertisements失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除Advertisements
// @Summary 删除Advertisements
// @Description 删除Advertisements
// @Tags Advertisements
// @Param data body dto.AdvertisementsDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/advertisements [delete]
// @Security Bearer
func (e Advertisements) Delete(c *gin.Context) {
	s := service.Advertisements{}
	req := dto.AdvertisementsDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除Advertisements失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}
